﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Text.RegularExpressions;


public class UGUITools : Editor {
    
    #region 快捷键移动
    [MenuItem("Plateface/Move/Up #W")]
    public static void UGUIMoveUp()
    {
        if (Selection.gameObjects.Length > 0)
        {
            foreach (var item in Selection.gameObjects) 
            {
                RectTransform tempRect = item.GetComponent<RectTransform>();
                tempRect.anchoredPosition
                    = new Vector2(tempRect.anchoredPosition.x, tempRect.anchoredPosition.y + 1);
            }
        }
    }

    [MenuItem("Plateface/Move/Down #S")]
    public static void UGUIMoveDowm()
    {
        if (Selection.gameObjects.Length > 0)
        {
            foreach (var item in Selection.gameObjects)
            {
                RectTransform tempRect = item.GetComponent<RectTransform>();
                tempRect.anchoredPosition
                    = new Vector2(tempRect.anchoredPosition.x, tempRect.anchoredPosition.y - 1);
            }
        }
    }

    [MenuItem("Plateface/Move/Left #A")]
    public static void UGUIMoveLeft()
    {
        if (Selection.gameObjects.Length > 0)
        {
            foreach (var item in Selection.gameObjects)
            {

                RectTransform tempRect = item.GetComponent<RectTransform>();
                if (tempRect != null)
                {
                    tempRect.anchoredPosition
                        = new Vector2(tempRect.anchoredPosition.x - 1, tempRect.anchoredPosition.y);
                }

                Transform rt = item.GetComponent<Transform>();
                if (rt != null)
                {
                    rt.position
                        = new Vector2(rt.position.x - 1, rt.position.y);
                }


            }
        }
    }

    [MenuItem("Plateface/Move/Right #D")]
    public static void UGUIMoveRight()
    {
        if (Selection.gameObjects.Length > 0)
        {
            foreach (var item in Selection.gameObjects) 
            {
                RectTransform tempRect = item.GetComponent<RectTransform>();
                tempRect.anchoredPosition
                    = new Vector2(tempRect.anchoredPosition.x + 1, tempRect.anchoredPosition.y);
            }
        }
    }
    #endregion
    
    #region 快捷键对齐
    [MenuItem("Plateface/Align/Up #I")]
    public static void Up()
    {
        if (Selection.gameObjects.Length > 1)
        {
            RectTransform a = Selection.gameObjects[0].GetComponent<RectTransform>();
            RectTransform tempRect = null;
            for (int i = 1; i < Selection.gameObjects.Length; i++)
            {
                tempRect = Selection.gameObjects[i].GetComponent<RectTransform>();
                tempRect.anchoredPosition = new Vector2(tempRect.anchoredPosition.x, a.anchoredPosition.y);
            }


        }
    }

    [MenuItem("Plateface/Align/Left #O")]
    public static void Left()
    {
        if (Selection.gameObjects.Length > 1)
        {
            RectTransform a = Selection.gameObjects[0].GetComponent<RectTransform>();


            RectTransform tempRect = null;
            for (int i = 1; i < Selection.gameObjects.Length; i++)
            {
                tempRect = Selection.gameObjects[i].GetComponent<RectTransform>();
                tempRect.anchoredPosition = new Vector2(a.anchoredPosition.x, tempRect.anchoredPosition.y);
            }
        }
    }

    [MenuItem("Plateface/Align/HorizaontalInterval #P")]
    public static void Interval()
    {
        if (Selection.gameObjects.Length > 1)
        {
            RectTransform a = Selection.gameObjects[0].GetComponent<RectTransform>();
            RectTransform tempRect = null;

            float width = 0f;

            for (int i = 1; i < Selection.gameObjects.Length; i++)
            {
                tempRect = Selection.gameObjects[i].GetComponent<RectTransform>();

                if (i == 1)
                {
                    width += a.anchoredPosition.x + a.sizeDelta.x + UGUIToolsWindow.interval;
                }
                else
                {
                    width += tempRect.sizeDelta.x + UGUIToolsWindow.interval;
                }
                tempRect.anchoredPosition = new Vector2(width, tempRect.anchoredPosition.y);
            }
        }   
    }

    [MenuItem("Plateface/OpenUGUIToolsWinodw %F1")]
    public static void OpenInterval()
    {
        EditorWindow.GetWindow<UGUIToolsWindow>(false,"UGUI工具窗口",false);
    }




    
    #endregion
}

/// <summary>
/// UGUI工具公共窗口
/// </summary>
public class UGUIToolsWindow : EditorWindow
{
    public static float interval = 0f;
    private float temp;

    static UGUIToolsWindow(){
        interval = PlayerPrefs.GetFloat("UGUIInterval");
    }

    public UGUIToolsWindow()
    {
        this.maxSize = new Vector2(500f, 500f);
        interval = PlayerPrefs.GetFloat("UGUIInterval");
        temp = interval;
    }

    public void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("A,B控件的间隔距离: ", GUILayout.MaxWidth(150));
        interval = EditorGUILayout.FloatField(interval, GUILayout.MaxWidth(50));
        if (temp != interval)
        {
            PlayerPrefs.SetFloat("UGUIInterval", interval);
        }

        if(GUILayout.Button("水平间隔排列",GUILayout.Width(100),GUILayout.Height(20)))
        {
            if (Selection.gameObjects.Length > 1)
            {
                GameObject [] array = GetSelectionGameObjectByNumber();
                if (array == null)
                    return;

                RectTransform a = array[0].GetComponent<RectTransform>();
                RectTransform tempRect = null;

                float width = 0f;

                for (int i = 1; i < array.Length; i++)
                {
                    tempRect = array[i].GetComponent<RectTransform>();

                    if (i == 1)
                    {
                        width += a.localPosition.x + a.sizeDelta.x + UGUIToolsWindow.interval;
                    }
                    else
                    {
                        width += a.sizeDelta.x + UGUIToolsWindow.interval;
                    }
                    tempRect.localPosition = new Vector2(width, tempRect.localPosition.y);
                }
            }   
        }

        if (GUILayout.Button("垂直间隔排列", GUILayout.Width(100), GUILayout.Height(20)))
        {
            if (Selection.gameObjects.Length > 1)
            {
                GameObject [] array = GetSelectionGameObjectByNumber();
                if (array == null)
                    return;

                RectTransform a = array[0].GetComponent<RectTransform>();
                RectTransform tempRect = null;

                float height = 0f;

                for (int i = 1; i < array.Length; i++)
                {
                    tempRect = array[i].GetComponent<RectTransform>();

                    if (i == 1)
                    {
                        height = a.anchoredPosition.y - (a.sizeDelta.y + UGUIToolsWindow.interval);
                    }
                    else
                    {
                        height -= (tempRect.sizeDelta.y + UGUIToolsWindow.interval);
                    }
                    tempRect.anchoredPosition = new Vector2(tempRect.anchoredPosition.x,height);
                }
            }   
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("垂直间隔排列", GUILayout.Width(100), GUILayout.Height(20)))
        {
            if (Selection.gameObjects.Length > 1)
            {
                GameObject[] array = GetSelectionGameObjectByNumber();
                if (array == null)
                    return;

                RectTransform curRect = null;
                RectTransform previousRect = null;

                float height = 0f;

                for (int i = 1; i < array.Length; i++)
                {
                    curRect = array[i].GetComponent<RectTransform>();
                    previousRect = array[i - 1].GetComponent<RectTransform>();

                    height = previousRect.anchoredPosition.y - (previousRect.sizeDelta.y + UGUIToolsWindow.interval);
                    curRect.anchoredPosition = new Vector2(curRect.anchoredPosition.x, height);
                }
            }
        }
        EditorGUILayout.EndHorizontal();
        


    }

    private GameObject[] GetSelectionGameObjectByParent() 
    {
        GameObject[] arys = Selection.gameObjects;
        GameObject[] childs = null;
        if (arys.Length == 1)
        {
            GameObject selectionObj = arys[0];
            int count = arys[0].transform.childCount;
            childs = new GameObject[count];

            for (int i = 0; i < count; i++)
            {
                childs[i] = selectionObj.transform.GetChild(i).gameObject;
                Debug.Log(childs[i].name);
            }
        }

        return childs;
    }

    private GameObject[] GetSelectionGameObjectByNumber()
    {
        GameObject[] array = Selection.gameObjects;
        GameObject tempObj = null;

        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length; j++)
            {
                if (StringExtractNumber(array[i].name) > StringExtractNumber(array[j].name))
                {
                    tempObj = array[i];
                    array[i] = array[j];
                    array[j] = tempObj;
                }
            }
        }
        return array;
    }

    private int StringExtractNumber(string str) 
    {
        string regex = @"\d{1,}";
        Match mstr = Regex.Match(str, regex);
        return System.Convert.ToInt32(mstr.Groups[0].Value);
    }
    
    
    
}
